package org.myself.meeting.controller;

import org.myself.meeting.Utils.Constant;
import org.myself.meeting.model.*;
import org.myself.meeting.service.EmployeeService;
import org.myself.meeting.service.MeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class ManagerController {

    @Autowired
    EmployeeService employeeService;

    @Autowired
    MeetingService meetingService;


    //注册审批
    @RequestMapping("/approval/register")
    @ResponseBody
    public Result approveaccount(@RequestBody Base base){
        //待通过的用户展示
        List<Employee> ls = employeeService.getEmpsBystatus(Constant.PUPASSSTATUS);
        Map<String,List<Employee>> map = new HashMap<String, List<Employee>>();
        map.put("emps",ls);
        Employee emp =employeeService.getEmpsByUsername(base.getUsername()) ;

        //升职中的用户展示
        //当前的用户是管理员的时候 打印升职状态的用户
        if(emp.getRole() == Constant.THREEROlE){
            List<Employee> lsup = employeeService.getEmpsBystatus(Constant.PUPSTATUS);
            map.put("lsup",lsup);
        }
        return new Result(200,"success",map);
    }
    //审批员工
    @RequestMapping("/updatestatus")
    @ResponseBody
    public Result updateStatus(@RequestBody UpdateStatusAcc acc){
        Employee emp =employeeService.getEmpsByUsername(acc.getUsername()) ;
        //管理员都可以审批员工
        Integer value = employeeService.updateStatus(acc.getEmployeeid(),acc.getStatus());
        return new Result(200,"success",null);
    }
    //审批升职
    @RequestMapping("/updatepromoted")
    @ResponseBody
    public Result updatePromoted(@RequestBody UpdateStatusAcc acc){
        //验证是否是管理员
        Employee emp =employeeService.getEmpsByUsername(acc.getUsername()) ;
        Employee empobject = employeeService.getEmpById(acc.getEmployeeid());
        if (emp.getRole() < Constant.THREEROlE){
            return new Result(400,"审批权限不够",null);
        }
        Integer value = employeeService.dealPromoted(acc.getEmployeeid(),empobject.getRole(),acc.getStatus(),emp);

        return new Result(200,"success",null);
    }

    //搜索员工的功能
    @RequestMapping("/searchemployees")
    @ResponseBody
    public Result getALlEmployees(@RequestBody SearchChemployees acc){
        //获取所有的员工
        List<Employee> emps = employeeService.getAllEmps(acc.getEmployee(),acc.getPage(),Constant.PAGESIZE);
        Long total = employeeService.getTotal(acc.getEmployee());
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("employee",acc.getEmployee());
        map.put("emps",emps);
        map.put("total",total);
        map.put("page",acc.getPage());
        map.put("pagenum",total%Constant.PAGESIZE==0?total/Constant.PAGESIZE : total/Constant.PAGESIZE+1 );
        return new Result(200,"success",map);
    }

    //通过id更新用户的状态
    @RequestMapping("/updateempbyid")
    @ResponseBody
    public Result updateempbyid(@RequestBody UpdateempbyidAcc acc ){
        Integer result = employeeService.updateStatus(acc.getId(),2);
        return new Result(200,"success",null);
    }

    //会议的审核
    @RequestMapping("/examinemeeting")
    @ResponseBody
    public Result examineMeeting(@RequestBody ExaminemeetingAcc acc){
        Map<String,List<SearchMeeting>> map = new HashMap<String, List<SearchMeeting>>();
        //待通过的会议
        List<SearchMeeting> lsmt1 = meetingService.getAllmeetings(acc.getPages(),Constant.PAGESIZE,Constant.TRIALSTATUS);
        map.put("passmts",lsmt1);
        //取消的会议
        List<SearchMeeting> lsmt2 = meetingService.getAllmeetings(acc.getPages(),Constant.PAGESIZE,Constant.CANCELTRIALSTATUS);
        map.put("cancelmts",lsmt2);
        return new Result(200,"success",map);
    }


    //会议状态的更改
    @RequestMapping("/updatemetstatus")
    @ResponseBody
    public Result updateMetStatus(@RequestBody UpdatemetstatusAcc acc){
        Employee emp =employeeService.getEmpsByUsername(acc.getUsername()) ;
        Integer value = meetingService.updateMetStatus(acc.getMeetingid(),acc.getStatus(),emp);

        return new Result(200,"success",null);
    }
}
